from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Software, Version
from .serializers import VersionSerializer
from django.shortcuts import get_object_or_404
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi


class CheckUpdateView(APIView):
    """
    检查软件更新的API端点
    """
    
    @swagger_auto_schema(
        operation_description="检查软件是否有新版本可用",
        manual_parameters=[
            openapi.Parameter(
                'software',
                openapi.IN_QUERY,
                description="软件名称",
                type=openapi.TYPE_STRING,
                required=True
            ),
            openapi.Parameter(
                'version',
                openapi.IN_QUERY,
                description="当前版本号",
                type=openapi.TYPE_STRING,
                required=True
            ),
        ],
        responses={
            200: openapi.Response(
                description="成功",
                examples={
                    "application/json": {
                        "has_update": True,
                        "version": {
                            "version_number": "2.0.0",
                            "download_url": "http://example.com/download",
                            "release_notes": "版本更新说明",
                            "is_force_update": False,
                            "min_version": "1.0.0"
                        }
                    }
                }
            ),
            400: "缺少必要参数",
            404: "软件不存在"
        }
    )
    def get(self, request):
        """
        获取软件的最新版本信息
        
        参数:
            software (string): 软件名称
            version (string): 当前版本号
            
        返回:
            {
                "has_update": bool,  # 是否有更新
                "version": {  # 如果有更新，则包含版本信息
                    "version_number": string,  # 版本号
                    "download_url": string,    # 下载地址
                    "release_notes": string,   # 发布说明
                    "is_force_update": bool,   # 是否强制更新
                    "min_version": string      # 最低兼容版本
                }
            }
        """
        software_name = request.GET.get('software')
        current_version = request.GET.get('version')
        
        if not software_name or not current_version:
            return Response({
                'error': '缺少必要参数'
            }, status=400)
            
        software = get_object_or_404(Software, name=software_name)
        latest_version = Version.objects.filter(
            software=software,
            status='published'
        ).order_by('-created_at').first()
        
        if not latest_version:
            return Response({
                'has_update': False,
                'message': '没有可用更新'
            })
            
        if latest_version.version_number <= current_version:
            return Response({
                'has_update': False,
                'message': '已经是最新版本'
            })
            
        serializer = VersionSerializer(latest_version)
        return Response({
            'has_update': True,
            'version': serializer.data
        }) 